/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.persistence.service.DataProcessService;
import com.lmx.repoDomain.tmodel.Call;
import com.lmx.repoDomain.tmodel.Workorder;
import com.lmx.repoDomain.tmodel.WorkorderAdd;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(WorkorderAdd.class)
@Component
public class WorkorderAddRepository extends BaseRepository<WorkorderAdd>{
    private Log logger = LogFactory.getLog(this.getClass());
    @Autowired
    private DataProcessService dataProcessService;

    @PostConstruct
    public void init() {
        logger.debug("init WorkorderAdd");
        dslJpaService.setEntityClass(WorkorderAdd.class);
    }


    @Override
    @JsonApiSave
    public WorkorderAdd save(WorkorderAdd workorderAdd) {
        if (workorderAdd.getId() == null) {
            int createTime = (int)(System.currentTimeMillis()/1000);
            workorderAdd.setCreateTime(createTime);
            workorderAdd = (WorkorderAdd)dslJpaService.create(workorderAdd);
            Workorder workorder = workorderAdd.getWorkorder();
            workorder.setUpdateTime(createTime);
            dataProcessService.updateWorkorder(workorder);
        } else {
//            dslJpaService.update(workorderAdd);
            workorderAdd.setErrcode(ICommonConstants.INTERFACEERRO.NOINTERFACE.ordinal());
        }
        return workorderAdd;
    }

}
